﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using DTO = BookClubIt.DTO;

namespace BookClubIt
{
    public partial class browsebookclubs : BasePage
    {
        

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {
                if (Request.QueryString["genre"] != null)
                {
                    int genreid = Convert.ToInt32(Request.QueryString["genre"]);
                 
                    DTO.BookClubs clubs = BookClubProcessorInstance.GetBookClubsByGenre(genreid);
                    grdBrowseClubs.DataSource = clubs;
                    grdBrowseClubs.DataBind();

                }
                else
                {
                    DTO.BookClubs clubs = BookClubProcessorInstance.GetActiveBookClubs();
                    grdBrowseClubs.DataSource = clubs;
                    grdBrowseClubs.DataBind();

                    DTO.GenreTypes genres = BookClubProcessorInstance.GetGenreTypes();
                    rptGenres.DataSource = genres;
                    rptGenres.DataBind();


                }
              

            }

             
        }

        public void BindData(DTO.BookClubs clubs)
        {
            grdBrowseClubs.DataSource = clubs;
            grdBrowseClubs.DataBind();
        }

        protected void grdBrowseClubs_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {


            DTO.BookClub club = e.Row.DataItem as DTO.BookClub;

            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image imgClubAvatar = ((Image)e.Row.FindControl("imgClubAvatar"));
                Image imgCurrentBook = ((Image)e.Row.FindControl("imgCurrentBook"));
                HyperLink lnkBookClub = ((HyperLink)e.Row.FindControl("lnkBookClub"));
                HyperLink lnkViewClub = ((HyperLink)e.Row.FindControl("lnkViewClub"));
                Label lblClubType = ((Label)e.Row.FindControl("lblClubType"));
                Label lblNumMembers = ((Label)e.Row.FindControl("lblNumMembers"));
                  Label lblCurrentBook = ((Label)e.Row.FindControl("lblCurrentBook"));
                  Label lblCreatedDate = ((Label)e.Row.FindControl("lblCreatedDate"));
                  Label lblGenreType = ((Label)e.Row.FindControl("lblGenreType"));

               int clubTypeInt = club.ClubType;
               int genreTypeInt = club.GenreType;
               int clubId = club.Id;
           //    string currentASIN = club.CurrentBook;

               DTO.ClubType type = BookClubProcessorInstance.GetBookClubType(clubTypeInt);
               DTO.GenreType gtype = BookClubProcessorInstance.GetBookClubGenre(genreTypeInt);
               DTO.Members members = MemberProcessorInstance.GetBookClubMembersById(clubId);
               DTO.Book current_book = BookProcessorInstance.GetCurrentBook(clubId);
           //    DTO.Book book = BookProcessorInstance.GetBookByASIN();

               imgClubAvatar.ImageUrl = "avatars/club/" + club.AvatarURL;
               lnkBookClub.Text = club.Name;
               lnkBookClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;
               lnkViewClub.NavigateUrl = "bookclub/default.aspx?id=" + club.Id;

               lblCurrentBook.Text = current_book.Title;
               imgCurrentBook.ImageUrl = current_book.SmallImageURL;
                lblCreatedDate.Text = club.CreatedDate.ToString("MMM dd, yyyy");
                lblGenreType.Text = gtype.GenreName;
                if(type.ClubTypeName == "Public")
                  lblClubType.CssClass = "status public";

                if (type.ClubTypeName == "Private")
                    lblClubType.CssClass = "status private";

                lblClubType.Text = type.ClubTypeName;

                lblNumMembers.Text = members.Count.ToString();

            }
            
        }
        protected void grdBrowseClubs_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdBrowseClubs.PageIndex = e.NewPageIndex;
            DTO.BookClubs clubs = BookClubProcessorInstance.GetActiveBookClubs();
            grdBrowseClubs.DataSource = clubs;
            grdBrowseClubs.DataBind();
        }

        protected void rptGenres_OnItemDataBound(object sender, RepeaterItemEventArgs e)
        {

            RepeaterItem item = e.Item; if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                DTO.GenreType genre = e.Item.DataItem as DTO.GenreType;

                HyperLink lnkGenre = (HyperLink)item.FindControl("lnkGenre");
                DTO.BookClubs clubs = BookClubProcessorInstance.GetBookClubsByGenre(genre.Id);

                int clubCount = clubs.Count;

                lnkGenre.Text = genre.GenreName + " (" + clubCount + ")";
                lnkGenre.NavigateUrl = "browsebookclubs.aspx?genre=" + genre.Id;

            }

        }

        static string TimeAgo(DateTime time)
        {
            DateTime startDate = DateTime.Now;
            TimeSpan deltaMinutes = startDate.Subtract(time);
            string distance = string.Empty;
            if (deltaMinutes.TotalMinutes <= (8724 * 60))
            {
                distance = DistanceOfTimeInWords(deltaMinutes.TotalMinutes);
                if (deltaMinutes.Minutes < 0)
                {
                    return distance + " from  now";
                }
                else
                {
                    return distance + " ago";
                }
            }
            else
            {
                return "on " + time.ToString();
            }
        }
        static string DistanceOfTimeInWords(double minutes)
        {
            if (minutes < 1)
            {
                return "less than a minute";
            }
            else if (minutes < 50)
            {
                return Math.Round(minutes).ToString() + " minutes";
            }
            else if (minutes < 90)
            {
                return "about one hour";
            }
            else if (minutes < 1080)
            {
                return Math.Round(new decimal((minutes / 60))).ToString() + " hours";
            }
            else if (minutes < 1440)
            {
                return "one day";
            }
            else if (minutes < 2880)
            {
                return "about one day";
            }
            else
            {
                return Math.Round(new decimal((minutes / 1440))).ToString() + " days";
            }
        }

    }
}